package com.ymt.order.controller;

import com.ymt.enums.PayType;
import com.ymt.order.service.OrderService;
import com.ymt.order.service.impl.OrderServiceImpl;
import com.ymt.order.service.impl.OrderServiceReliableMessageImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
    @Autowired
    private OrderServiceReliableMessageImpl orderService2;
    @Autowired
    private OrderServiceImpl orderService1;

    @GetMapping(value = "/payOrder2")
    private String payOrder2(Integer threds) {
        initThreds(threds, orderService2);
        return "success";
    }

    @GetMapping(value = "/payOrder")
    private String payOrder(Integer threds) {
        initThreds(threds, orderService1);
        return "success";
    }

    private void initThreds(Integer threds, OrderService orderService) {
        for (PayType payType : PayType.values()) {
            for (int i = 0; i < threds; i++) {
                PayOrderThred payOrderThred1 = PayOrderThred.create(payType, orderService);
                Thread thread1 = new Thread(payOrderThred1);
                thread1.setName(payType.getPayName() + (i + 1));
                thread1.start();
            }
        }
    }
}
